package com.Jshop.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 消息队列定义
 * @Param
 * @return
 * @Date  2021/11/11
 **/
@Configuration
public class MqConfig {


    @Bean
    public Exchange directDelayExchange() {
        return new DirectExchange("directDelayExchange");
    }

    @Bean
    public Queue orderDelayQueue() {
//        return new Queue("orderDelayQueue", true);
        return QueueBuilder.durable("orderDelayQueue")
                .withArgument("x-dead-letter-exchange","orderListenerExchange")
                .withArgument("x-dead-letter-routing-key","orderListenerRoutingKey") //绑定指定的routing-key
                .build();
    }

    @Bean
    public Binding orderDelayBinding(Queue orderDelayQueue, Exchange directDelayExchange) {
        return BindingBuilder
                .bind(orderDelayQueue)
                .to(directDelayExchange)
                .with("orderDelayQueueKey")
                .noargs();
    }

    /**
     * 创建交换机 队列1消息过期后 消息会发送至该交换机 该交换机会转发送给队列2
     * 交换机模式为direct
     * @return
     */
    @Bean
    public Exchange orderListenerExchange() {
        return new DirectExchange("orderListenerExchange");
    }

    @Bean
    public Queue orderListenerQueue() {
        return new Queue("orderListenerQueue",true);
    }



    /**
     * 队列queue2绑定exchange
     * @param orderListenerQueue
     * @param orderListenerExchange
     * @return
     */
    @Bean
    public Binding orderListenerBinding(Queue orderListenerQueue, Exchange orderListenerExchange) {
        return BindingBuilder
                .bind(orderListenerQueue)
                .to(orderListenerExchange)
                .with("orderListenerRoutingKey")
                .noargs();
    }


    @Bean
    public Exchange secKillExchange() {
        return new DirectExchange("secKillExchange");
    }

    @Bean
    public Queue secKillQueue() {
        return new Queue("secKillQueue",true);
    }


    @Bean
    public Binding secKillBinding(Queue secKillQueue, Exchange secKillExchange) {
        return BindingBuilder
                .bind(secKillQueue)
                .to(secKillExchange)
                .with("secKillOrder")
                .noargs();
    }




}
